package org.practice.action;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.practice.model.Student;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.opensymphony.xwork2.ActionSupport;

/**
 * json-lib-2.2.3-jdk15.jar,ezmorph-1.0.6.jar,commons-lang.jar
 * commons-beanutils.jar,commons-collections.jar,commons-logging.jar 
 */
public class TestJsonAction extends ActionSupport{
	private String operType;
	@Override
	public String execute() throws Exception {
		if(operType!=null&&"ajax".equals(operType)){
			HttpServletResponse response=ServletActionContext.getResponse();
			response.setHeader("pragma","no-cache");
			response.setHeader("cache-control","no-cache");
			response.setContentType("text/html;charset=UTF-8");//设置ajax响应字符编码，可以req.responseText防止乱码
			response.setDateHeader("Expires", 0);
			PrintWriter pw=response.getWriter();
			pw.write(createJson());
			return null;
		}
		System.out.println("=====operate");
		return SUCCESS;
	}
	private String createJson(){
//		List<Student> allStudents=new ArrayList<Student>();    
//		allStudents.add(new Student(new Long(1),"20034140201","张三1","男",25));    
//		allStudents.add(new Student(new Long(2),"20034140202","张三2","女",26));    
//		System.out.println("json===="+JSONArray.fromObject(allStudents).toString());
		
		JSONObject jsonObject = new JSONObject();   
        jsonObject.put("title","上海图书馆");   
        //输出jsonobject对象   
        System.out.println("jsonObject==>"+jsonObject);   
        
        //返回一个JSONArray对象   
        JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject1 = new JSONObject();
        JSONObject jsonObject11 = new JSONObject();
        jsonObject11.put("title","张先生");
        jsonObject11.put("name","张三");
        jsonObject1.put("author", jsonObject11);
        JSONObject jsonObject12 = new JSONObject();
        jsonObject12.put("title","java");
        jsonObject12.put("price","$22.95");
        jsonObject1.put("book", jsonObject12);
        
        JSONObject jsonObject2 = new JSONObject();
        JSONObject jsonObject21 = new JSONObject();
        jsonObject21.put("title","李先生");
        jsonObject21.put("name","李四");
        jsonObject2.put("author", jsonObject21);
        JSONObject jsonObject22 = new JSONObject();
        jsonObject22.put("title","C++");
        jsonObject22.put("price","$33.95");
        jsonObject2.put("book", jsonObject22);
        
        jsonArray.add(0,jsonObject1);   
        jsonArray.add(1,jsonObject2);   
        jsonObject.element("signArray",jsonArray);   
        System.out.println("结果="+jsonObject); 
        return jsonObject.toString();
	}
	public String getOperType() {
		return operType;
	}
	public void setOperType(String operType) {
		this.operType = operType;
	}
	
}
